package com.magist.manage.web.service.impl;

import com.magist.core.service.SimpleServiceImpl;
import com.magist.manage.web.entity.SysRole;
import com.magist.manage.web.entity.UserAuth;
import com.magist.manage.web.service.SysAuthService;
import com.magist.manage.web.service.SysRoleService;
import com.magist.manage.web.service.UserAuthService;
import com.magist.manage.web.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

@Service
public class UserAuthServiceImpl extends SimpleServiceImpl<UserAuth> implements UserAuthService{

    @Autowired
    private SysAuthService sysAuthService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private SysRoleService sysRoleService;

    @Override
    public String getRoleGroupName(String roleGroup) {
        if(StringUtils.isEmpty(roleGroup)) {
            return null;
        }
        String roleGroupName = "";
        String[] roles = roleGroup.split("#");
        for (String role : roles) {
            SysRole sysRole = sysRoleService.getById(role);
            if(ObjectUtils.isEmpty(sysRole)) {
                continue;
            }
            String name = sysRole.getRoleName();
            if(!StringUtils.isEmpty(name)) {
                roleGroupName += name;
            }
        }
        return roleGroupName;
    }

    @Override
    public String getRoleGroupNameByUserId(String userId) {
        UserAuth userAuth = this.getById(userId);
        if(ObjectUtils.isEmpty(userAuth)) {
            return null;
        }
        return this.getRoleGroupName(userAuth.getRoleGroup());
    }
}